<?php
/**
 * POSCMS框架文件
 * 二次开发时请勿修改本文件
 * 四川迅睿云软件开发有限公司 www.xunruicms.com
 */
	
class Application extends M_Controller {

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
		$this->template->assign('menu', $this->get_menu_v3(array(
		    fc_lang('本地插件') => array('admin/application/index', 'puzzle-piece'),
            fc_lang('更新缓存') => array('admin/home/cache', 'refresh'),
            fc_lang('插件市场') => array('admin/api/cjonline', 'cloud'),
		)));
		\Poscms\Service::M('application');
    }
	
	/**
     * 管理
     */
    public function index() {
	
		$store = $data = array();
		$path = APPSPATH;
		defined('MYAPPSPATH') && $path = MYAPPSPATH;
		$local = dr_dir_map($path, 1); // 搜索本地应用
		$application = $this->application_model->get_data(); // 库中已安装应用
		
		if ($local) {
			foreach ($local as $dir) {
			    $path = dr_get_app_dir($dir);
                if (is_file($path.'Config/Version.php')
                    && is_file($path.'Config/App.php')) {
                    $ver = require $path.'Config/Version.php';
                    // 兼容phpcmf写法
                    $cfg = require $path.'Config/App.php';

                    $app = array(
                        'phpcmf'      => 1,
                        'key'			=> $ver['id'], // 官方应用商城上线的id号
                        'name'			=> $cfg['name'],
                        'author'		=> $cfg['author'],
                        'version'		=> $ver['version'],
                    );
                    if (isset($application[$dir])) {
                        $config = $data[1][$dir] = array_merge($application[$dir], $app);
                        $config['key'] && (
                        isset($store[$config['key']])
                            ? (version_compare($config['version'], $store[$config['key']], '<') && $store[$config['key']] = $config['version'])
                            : $store[$config['key']] = $config['version']
                        );
                    } else {
                        $data[0][$dir] = $app;
                    }
                } elseif (is_file($path.'config/app.php')) {
				    // poscms插件
					if (isset($application[$dir])) {
						$config = $data[1][$dir] = array_merge($application[$dir], require $path.'config/app.php');
						$config['key'] && (
							isset($store[$config['key']])
								? (version_compare($config['version'], $store[$config['key']], '<') && $store[$config['key']] = $config['version'])
								: $store[$config['key']] = $config['version']
						);
					} else {
						$data[0][$dir] = require $path.'config/app.php';
					}
				}
			}
		}

		$this->template->assign(array(
			'list' => $data,
			'store' => dr_array2string($store),
		));
		$this->template->display('application_index.html');
    }

    // 安装插件
    public function install() {

        $dir = dr_safe_filename($_GET['dir']);
        $path = dr_get_app_dir($dir);
        if (is_file($path.'Config/Version.php')
            && is_file($path.'Config/App.php')) {
            // 安装
            if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
                $rt = \Poscms\Service::M('app')->install($dir);
                if ($rt['code']) {
                    $this->admin_msg($rt['msg'], dr_url('application/index'), 1);
                } else {
                    $this->admin_msg($rt['msg']);
                }
            } else {
                $this->admin_msg(fc_lang('PHP环境要求7.0以上，当前PHP环境是：'.PHP_VERSION));
            }
        } else {
            $this->admin_msg(fc_lang('正在安装插件'), dr_url($dir.'/home/install'), 2);
        }
    }

    // 卸载插件
    public function uninstall() {

        $dir = dr_safe_filename($_GET['dir']);
        $path = dr_get_app_dir($dir);
        if (version_compare(PHP_VERSION, '7.0.0') >= 0
            && is_file($path.'Config/Version.php')
            && is_file($path.'Config/App.php')) {
            // 卸载
            $rt = \Poscms\Service::M('app')->uninstall($dir);
            if ($rt['code']) {
                $this->admin_msg($rt['msg'], dr_url('application/index'), 1);
            } else {
                $this->admin_msg($rt['msg']);
            }
        } else {
            $this->admin_msg(fc_lang('正在卸载插件'), dr_url($dir.'/home/uninstall'), 2);
        }
    }
    
	/**
     * 禁用/可用
     */
    public function disabled() {
	
		if ($this->is_auth('admin/application/config')) {
			$id = (int)$this->input->get('id');
			$data = $this->db->where('id', $id)->get('application')->row_array();
            $value = $data['disabled'] == 1 ? 0 : 1;
			$this->db->where('id', $id)->update('application', array(
                'disabled' => $value
            ));
            $this->clear_cache('app');
            $this->system_log(($value ? '禁用' : '启用').'应用【'.$data['dirname'].'】'); // 记录日志
		}
		
		exit(dr_json(1, fc_lang('操作成功')));
    }
	
	/**
     * 删除
     */
    public function delete() {

		$this->admin['adminid'] != 1 && $this->admin_msg(fc_lang('您无权限操作(%s)', 'delete'));

		$dir = $this->input->get('dir');

        $this->load->helper('file');
		delete_files(APPSPATH.$dir.'/', TRUE);

		is_dir(APPSPATH.$dir.'/') && @rmdir(APPSPATH.$dir.'/');

		is_dir(APPSPATH.$dir.'/') && $this->admin_msg(fc_lang('无文件删除权限，建议通过FTP等工具删除此目录'));

        // 删除菜单
        $this->db->where('mark', 'app-'.$dir)->delete('admin_menu');
        $this->db->where('mark', 'app-'.$dir)->delete('member_menu');

        $this->clear_cache('app');
        $this->system_log('删除应用【'.$dir.'】'); // 记录日志

		$this->admin_msg(fc_lang('操作成功，正在刷新...'), dr_url('application/index'), 1);
    }

    public function yun() {
        header('Location: http://www.xunruicms.com/shop/search-app-v-2-iscms-1.html');exit;
    }


    /**
     * 缓存
     */
    public function cache() {
        $this->application_model->cache();
        (int)$_GET['admin'] or $this->admin_msg(fc_lang('操作成功，正在刷新...'), isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', 1);
    }
}